package com.weiyi.mashangzhu.Roomer.Activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Bean.FlagBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ScreenActivity extends BaseActivity {

    @BindView(R.id.price_type_jj)
    RadioButton priceTypeJj;
    @BindView(R.id.price_type_ss)
    RadioButton priceTypeSs;
    @BindView(R.id.price_type_gd)
    RadioButton priceTypeGd;
    @BindView(R.id.price_type_hh)
    RadioButton priceTypeHh;
    @BindView(R.id.price_type)
    RadioGroup priceType;
    //    @BindView(R.id.house_type_jj)
//    RadioButton houseTypeJj;
//    @BindView(R.id.house_type_ss)
//    RadioButton houseTypeSs;
//    @BindView(R.id.house_type_gd)
//    RadioButton houseTypeGd;
//    @BindView(R.id.house_type_hh)
//    RadioButton houseTypeHh;
//    @BindView(R.id.housetype_type)
//    RadioGroup housetypeType;
    @BindView(R.id.jushi_type_jj)
    RadioButton jushiTypeJj;
    @BindView(R.id.jushi_type_ss)
    RadioButton jushiTypeSs;
    @BindView(R.id.jushi_type_gd)
    RadioButton jushiTypeGd;
    @BindView(R.id.jushi_type_hh)
    RadioButton jushiTypeHh;
    @BindView(R.id.jushi_type)
    RadioGroup jushiType;
    @BindView(R.id.count)
    TextView count;
    @BindView(R.id.jian)
    ImageView jian;
    @BindView(R.id.bed_type_jj)
    RadioButton bedTypeJj;
    @BindView(R.id.bed_type_ss)
    RadioButton bedTypeSs;
    //    @BindView(R.id.bed_type_gd)
//    RadioButton bedTypeGd;
    @BindView(R.id.bed_type_hh)
    RadioButton bedTypeHh;
    @BindView(R.id.bed_type)
    RadioGroup bedType;
    @BindView(R.id.wifi)
    TextView wifi;
    @BindView(R.id.hot)
    TextView hot;
    @BindView(R.id.washer)
    TextView washer;
    @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.air)
    TextView air;
    @BindView(R.id.life)
    TextView life;
    @BindView(R.id.cooking)
    TextView cooking;
    @BindView(R.id.smoking)
    TextView smoking;

    List<String> pricelist = new ArrayList<>();
    List<String> typelist = new ArrayList<>();

    List<String> facilist = new ArrayList<>();
    List<Boolean> flaglist = new ArrayList<>();
    @BindView(R.id.minju)
    TextView minju;
    @BindView(R.id.gongyu)
    TextView gongyu;
    @BindView(R.id.bieshu)
    TextView bieshu;
    @BindView(R.id.siheyuan)
    TextView siheyuan;
    @BindView(R.id.kezhan)
    TextView kezhan;
    @BindView(R.id.gelou)
    TextView gelou;
    //    @BindView(R.id.gridview)
//    GridView gridview;
    String person = null;
    String price = null;
    String type = null;
    String room = null;
    String bed_type = null;
    String facil = null;
    List<FlagBean> flags = new ArrayList<>(8);
    List<FlagBean> flags_type = new ArrayList<>(7);
    @BindView(R.id.qita)
    TextView qita;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen);
        ButterKnife.bind(this);
        for (int i = 0; i < 8; i++) {
            flaglist.add(false);
        }
        pricelist.add("150以下");
        pricelist.add("150-300");
        pricelist.add("300-500");
        pricelist.add("500以上");
        for (int i = 0; i < 8; i++) {
            flags.add(new FlagBean(false));
        }
        for (int i = 0; i < 7; i++) {
            flags_type.add(new FlagBean(false));
        }
        if (MyApp.price_xuan != null) {
            if (MyApp.price_xuan.equals("0,150")) {
                priceTypeJj.setChecked(true);

            } else if (MyApp.price_xuan.equals("150,300")) {
                priceTypeSs.setChecked(true);
            } else if (MyApp.price_xuan.equals("300,500")) {
                priceTypeGd.setChecked(true);
            } else if (MyApp.price_xuan.equals("500")) {
                priceTypeHh.setChecked(true);
            }
        }
        if (MyApp.flags_type_xuan != null) {
            for (int i = 0; i < MyApp.flags_type_xuan.size(); i++) {
                Log.e("bmmmmmm", MyApp.flags_type_xuan.get(i).isFlag() + "");
            }

            if (MyApp.flags_type_xuan.get(0).isFlag() == true) {
                setTextInfo2(minju, R.color.colorPrimary, R.drawable.shape_blue_line_bg);
                flags_type.get(0).setFlag(true);

            }
            if (MyApp.flags_type_xuan.get(1).isFlag() == true) {
                setTextInfo2(gongyu, R.color.colorPrimary, R.drawable.shape_blue_line_bg);
                flags_type.get(1).setFlag(true);

            }
            if (MyApp.flags_type_xuan.get(2).isFlag() == true) {
                setTextInfo2(bieshu, R.color.colorPrimary, R.drawable.shape_blue_line_bg);
                flags_type.get(2).setFlag(true);
            }
            if (MyApp.flags_type_xuan.get(3).isFlag() == true) {
                setTextInfo2(siheyuan, R.color.colorPrimary, R.drawable.shape_blue_line_bg);
                flags_type.get(3).setFlag(true);

            }
            if (MyApp.flags_type_xuan.get(4).isFlag() == true) {
                setTextInfo2(kezhan, R.color.colorPrimary, R.drawable.shape_blue_line_bg);
                flags_type.get(4).setFlag(true);

            }
            if (MyApp.flags_type_xuan.get(5).isFlag() == true) {
                setTextInfo2(gelou, R.color.colorPrimary, R.drawable.shape_blue_line_bg);
                flags_type.get(5).setFlag(true);
            }
            if (MyApp.flags_type_xuan.get(6).isFlag() == true) {
                setTextInfo2(qita, R.color.colorPrimary, R.drawable.shape_blue_line_bg);
                flags_type.get(6).setFlag(true);
            }
        }
        if (MyApp.room_xuan != null) {
            if (MyApp.room_xuan.equals("1")) {
                jushiTypeJj.setChecked(true);
            } else if (MyApp.room_xuan.equals("2")) {
                jushiTypeSs.setChecked(true);
            } else if (MyApp.room_xuan.equals("3")) {
                jushiTypeGd.setChecked(true);
            }
//           else  if (MyApp.room_xuan.equals("")){
//               jushiTypeHh.setChecked(true);
//           }
        }
        if (MyApp.person_xuan != null && !MyApp.person_xuan.equals("")) {
            count.setText(MyApp.person_xuan);
        } else {
            count.setText("1");
        }
        if (MyApp.bed_type_xuan != null) {
            if (MyApp.bed_type_xuan.equals("单")) {
                bedTypeJj.setChecked(true);

            } else if (MyApp.bed_type_xuan.equals("双")) {
                bedTypeSs.setChecked(true);
            }
//            else   if (MyApp.bed_type_xuan.equals("")){
//                bedTypeHh.setChecked(true);
//            }
        }
        if (MyApp.flags_xuan != null) {

            for (int i = 0; i < MyApp.flags_xuan.size(); i++) {
                switch (i) {
                    case 0:
                        if (MyApp.flags_xuan.get(i).isFlag() == true) {
                            setTextInfo(wifi, R.color.colorPrimary, R.mipmap.wifi1);
                            flags.get(i).setFlag(true);
                        }
                        break;
                    case 1:
                        if (MyApp.flags_xuan.get(i).isFlag() == true) {
                            setTextInfo(hot, R.color.colorPrimary, R.mipmap.hot241);
                            flags.get(i).setFlag(true);
                        }
                        break;
                    case 2:
                        if (MyApp.flags_xuan.get(i).isFlag() == true) {
                            setTextInfo(washer, R.color.colorPrimary, R.mipmap.washe1r);
                            flags.get(i).setFlag(true);
                        }
                        break;
                    case 3:
                        if (MyApp.flags_xuan.get(i).isFlag() == true) {
                            setTextInfo(tv, R.color.colorPrimary, R.mipmap.tv1);
                            flags.get(i).setFlag(true);
                        }
                        break;
                    case 4:
                        if (MyApp.flags_xuan.get(i).isFlag() == true) {
                            setTextInfo(air, R.color.colorPrimary, R.mipmap.air1);
                            flags.get(i).setFlag(true);
                        }
                        break;
                    case 5:
                        if (MyApp.flags_xuan.get(i).isFlag() == true) {
                            setTextInfo(life, R.color.colorPrimary, R.mipmap.lift1);
                            flags.get(i).setFlag(true);
                        }
                        break;
                    case 6:
                        if (MyApp.flags_xuan.get(i).isFlag() == true) {
                            setTextInfo(cooking, R.color.colorPrimary, R.mipmap.cooking1);
                            flags.get(i).setFlag(true);
                        }
                        break;
                    case 7:
                        if (MyApp.flags_xuan.get(i).isFlag() == true) {
                            setTextInfo(smoking, R.color.colorPrimary, R.mipmap.smoking1);
                            flags.get(i).setFlag(true);
                        }
                        break;
                }


            }
        }


        priceType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.price_type_jj:
                        price = "0,150";
                        break;
                    case R.id.price_type_ss:
                        price = "150,300";
                        break;
                    case R.id.price_type_gd:
                        price = "300,500";
                        break;
                    case R.id.price_type_hh:
                        price = "500";
                        break;
                }

            }
        });
        bedType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.bed_type_jj:
                        bed_type = "单";
                        break;
                    case R.id.bed_type_ss:
                        bed_type = "双";
                        break;
                    case R.id.bed_type_hh:
                        bed_type = "";
                        break;
                }

            }
        });
        jushiType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.jushi_type_jj:
                        room = "1";
                        break;
                    case R.id.jushi_type_ss:
                        room = "2";
                        break;
                    case R.id.jushi_type_gd:
                        room = "3";
                        break;
                    case R.id.jushi_type_hh:
                        room = "";
                        break;
                }

            }
        });
    }


    @OnClick({R.id.qita, R.id.minju, R.id.gongyu, R.id.bieshu, R.id.siheyuan, R.id.kezhan, R.id.gelou, R.id.back, R.id.reset, R.id.sure, R.id.jia, R.id.jian, R.id.wifi, R.id.hot, R.id.washer, R.id.tv, R.id.air, R.id.life, R.id.cooking, R.id.smoking})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.qita:
                if (flags_type.get(6).isFlag() == true) {
                    setTextInfo2(qita, R.color.selector_radio_text_color, R.drawable.shape_light_line_bg);
                    flags_type.get(6).setFlag(false);
                } else {
                    setTextInfo2(qita, R.color.colorPrimary, R.drawable.shape_blue_line_bg);
                    flags_type.get(6).setFlag(true);
                }

                break;
            case R.id.minju:
                if (flags_type.get(0).isFlag() == true) {
                    setTextInfo2(minju, R.color.selector_radio_text_color, R.drawable.shape_light_line_bg);
                    flags_type.get(0).setFlag(false);
                } else {
                    setTextInfo2(minju, R.color.colorPrimary, R.drawable.shape_blue_line_bg);
                    flags_type.get(0).setFlag(true);
                }

                break;
            case R.id.gongyu:
                if (flags_type.get(1).isFlag() == true) {
                    setTextInfo2(gongyu, R.color.selector_radio_text_color, R.drawable.shape_light_line_bg);
                    flags_type.get(1).setFlag(false);
                } else {
                    setTextInfo2(gongyu, R.color.colorPrimary, R.drawable.shape_blue_line_bg);
                    flags_type.get(1).setFlag(true);
                }
                break;
            case R.id.bieshu:
                if (flags_type.get(2).isFlag() == true) {
                    setTextInfo2(bieshu, R.color.selector_radio_text_color, R.drawable.shape_light_line_bg);
                    flags_type.get(2).setFlag(false);
                } else {
                    setTextInfo2(bieshu, R.color.colorPrimary, R.drawable.shape_blue_line_bg);
                    flags_type.get(2).setFlag(true);
                }
                break;
            case R.id.siheyuan:
                if (flags_type.get(3).isFlag() == true) {
                    setTextInfo2(siheyuan, R.color.selector_radio_text_color, R.drawable.shape_light_line_bg);
                    flags_type.get(3).setFlag(false);
                } else {
                    setTextInfo2(siheyuan, R.color.colorPrimary, R.drawable.shape_blue_line_bg);
                    flags_type.get(3).setFlag(true);
                }
                break;
            case R.id.kezhan:
                if (flags_type.get(4).isFlag() == true) {
                    setTextInfo2(kezhan, R.color.selector_radio_text_color, R.drawable.shape_light_line_bg);
                    flags_type.get(4).setFlag(false);
                } else {
                    setTextInfo2(kezhan, R.color.colorPrimary, R.drawable.shape_blue_line_bg);
                    flags_type.get(4).setFlag(true);
                }
                break;
            case R.id.gelou:
                if (flags_type.get(5).isFlag() == true) {
                    setTextInfo2(gelou, R.color.selector_radio_text_color, R.drawable.shape_light_line_bg);
                    flags_type.get(5).setFlag(false);
                } else {
                    setTextInfo2(gelou, R.color.colorPrimary, R.drawable.shape_blue_line_bg);
                    flags_type.get(5).setFlag(true);
                }
                break;
            case R.id.back:
                finish();
                break;
            case R.id.reset:
                priceTypeGd.setChecked(false);
                priceTypeHh.setChecked(false);
                priceTypeJj.setChecked(false);
                priceTypeSs.setChecked(false);
                price = "";
                setTextInfo2(gelou, R.color.selector_radio_text_color, R.drawable.shape_light_line_bg);
                setTextInfo2(gongyu, R.color.selector_radio_text_color, R.drawable.shape_light_line_bg);
                setTextInfo2(bieshu, R.color.selector_radio_text_color, R.drawable.shape_light_line_bg);
                setTextInfo2(siheyuan, R.color.selector_radio_text_color, R.drawable.shape_light_line_bg);
                setTextInfo2(kezhan, R.color.selector_radio_text_color, R.drawable.shape_light_line_bg);
                setTextInfo2(minju, R.color.selector_radio_text_color, R.drawable.shape_light_line_bg);
                for (int i = 0; i < flags_type.size(); i++) {
                    flags_type.get(i).setFlag(false);
                }
                typelist.clear();
                type = "";

                jushiTypeGd.setChecked(false);
                jushiTypeHh.setChecked(false);
                jushiTypeSs.setChecked(false);
                room = "";
                bedTypeHh.setChecked(false);
                bedTypeJj.setChecked(false);
                bedTypeSs.setChecked(false);
                bed_type = "";
                count.setText("1");
                person = "1";
                setTextInfo(hot, R.color.light_gray, R.mipmap.hot24);
                setTextInfo(wifi, R.color.light_gray, R.mipmap.wifi);
                setTextInfo(washer, R.color.light_gray, R.mipmap.washer);
                setTextInfo(life, R.color.light_gray, R.mipmap.lift);
                setTextInfo(cooking, R.color.light_gray, R.mipmap.cooking);
                setTextInfo(smoking, R.color.light_gray, R.mipmap.smoking);
                setTextInfo(air, R.color.light_gray, R.mipmap.air);
                setTextInfo(tv, R.color.light_gray, R.mipmap.tv);
                for (int i = 0; i < flags.size(); i++) {
                    flags.get(i).setFlag(false);

                }
                MyApp.flags_xuan.clear();
                facil = "";

                break;
            case R.id.sure:
                Log.e("VVVVVV", "fail=" + MyApp.facil_xuan + ",price=" + MyApp.price_xuan + ",type=" + MyApp.type_xuan + ",room=" + MyApp.room_xuan + ",bed=" + MyApp.bed_type_xuan + ",person=" + MyApp.person_xuan);

                facilist.clear();
                facil = "";
                for (int i = 0; i < flags.size(); i++) {
                    switch (i) {
                        case 0:
                            if (flags.get(i).isFlag() == true) {
                                facilist.add("1");
                            }
                            break;
                        case 1:
                            if (flags.get(i).isFlag() == true) {
                                facilist.add("3");
                            }
                            break;
                        case 2:
                            if (flags.get(i).isFlag() == true) {
                                facilist.add("5");
                            }
                            break;
                        case 3:
                            if (flags.get(i).isFlag() == true) {
                                facilist.add("2");
                            }
                            break;
                        case 4:
                            if (flags.get(i).isFlag() == true) {
                                facilist.add("8");
                            }
                            break;
                        case 5:
                            if (flags.get(i).isFlag() == true) {
                                facilist.add("6");
                            }
                            break;
                        case 6:
                            if (flags.get(i).isFlag() == true) {
                                facilist.add("11");
                            }
                            break;
                        case 7:
                            if (flags.get(i).isFlag() == true) {
                                facilist.add("13");
                            }
                            break;
                    }


                }
                if (facilist.size() >= 1) {
                    for (int i = 0; i < facilist.size() - 1; i++) {
                        facil += facilist.get(i) + ",";
                    }
                    facil += facilist.get(facilist.size() - 1);

                }
                MyApp.facil_xuan = facil;


                if (price != null) {
                    MyApp.price_xuan = price;
                }
                if (flags_type != null) {
                    MyApp.flags_type_xuan = flags_type;
                    for (int i = 0; i < flags_type.size(); i++) {
                        if (flags_type.get(i).isFlag() == true) {
                            typelist.add(i + 1 + "");
                        }
                    }
                    if (typelist.size() >= 1) {
                        type = "";
                        for (int i = 0; i < typelist.size() - 1; i++) {
                            type += typelist.get(i) + ",";
                        }
                        type += typelist.get(typelist.size() - 1);
                        MyApp.type_xuan = type;
                    } else {
                        MyApp.type_xuan = "";
                    }

                }
                if (room != null) {
                    MyApp.room_xuan = room;
                }
                if (bed_type != null) {
                    MyApp.bed_type_xuan = bed_type;
                }
                if (facil != null) {
                    MyApp.flags_xuan = flags;
                }
                if (person != null) {
                    MyApp.person_xuan = person;
                }
                Log.e("VVVVVV", "fail=" + MyApp.facil_xuan + ",price=" + MyApp.price_xuan + ",type=" + MyApp.type_xuan + ",room=" + MyApp.room_xuan + ",bed=" + MyApp.bed_type_xuan + ",person=" + MyApp.person_xuan);
                Intent intent = new Intent();
                setResult(0x003, intent);
                finish();
                break;
            case R.id.jia:
                count.setText(Integer.parseInt(count.getText() + "") + 1 + "");
                person = count.getText() + "";
                break;
            case R.id.jian:
                if ((count.getText() + "").equals("1")) {
                    jian.setImageResource(R.mipmap.jian);
                } else {
                    count.setText(Integer.parseInt(count.getText() + "") - 1 + "");
                    jian.setImageResource(R.mipmap.jian1);
                }
                person = count.getText() + "";
                break;
            case R.id.wifi:
                if (flags.get(0).isFlag() == false) {
                    setTextInfo(wifi, R.color.colorPrimary, R.mipmap.wifi1);
                    flags.get(0).setFlag(true);
                } else {
                    setTextInfo(wifi, R.color.light_gray, R.mipmap.wifi);
                    flags.get(0).setFlag(false);
                }


                break;
            case R.id.hot:
                if (flags.get(1).isFlag() == false) {
                    setTextInfo(hot, R.color.colorPrimary, R.mipmap.hot241);
                    flags.get(1).setFlag(true);
                } else {
                    setTextInfo(hot, R.color.light_gray, R.mipmap.hot24);
                    flags.get(1).setFlag(false);
                }
                break;
            case R.id.washer:
                if (flags.get(2).isFlag() == false) {
                    setTextInfo(washer, R.color.colorPrimary, R.mipmap.washe1r);
                    flags.get(2).setFlag(true);
                } else {
                    setTextInfo(washer, R.color.light_gray, R.mipmap.washer);
                    flags.get(2).setFlag(false);
                }
                break;
            case R.id.tv:
                if (flags.get(3).isFlag() == false) {
                    setTextInfo(tv, R.color.colorPrimary, R.mipmap.tv1);
                    flags.get(3).setFlag(true);
                } else {
                    setTextInfo(tv, R.color.light_gray, R.mipmap.tv);
                    flags.get(3).setFlag(false);
                }
                break;
            case R.id.air:
                if (flags.get(4).isFlag() == false) {
                    setTextInfo(air, R.color.colorPrimary, R.mipmap.air1);
                    flags.get(4).setFlag(true);
                } else {
                    setTextInfo(air, R.color.light_gray, R.mipmap.air);
                    flags.get(4).setFlag(false);
                }
                break;
            case R.id.life:
                if (flags.get(5).isFlag() == false) {
                    setTextInfo(life, R.color.colorPrimary, R.mipmap.lift1);
                    flags.get(5).setFlag(true);
                } else {
                    setTextInfo(life, R.color.light_gray, R.mipmap.lift);
                    flags.get(5).setFlag(false);
                }
                break;
            case R.id.cooking:
                if (flags.get(6).isFlag() == false) {
                    setTextInfo(cooking, R.color.colorPrimary, R.mipmap.cooking1);
                    flags.get(6).setFlag(true);
                } else {
                    setTextInfo(cooking, R.color.light_gray, R.mipmap.cooking);
                    flags.get(6).setFlag(false);
                }
                break;
            case R.id.smoking:
                if (flags.get(7).isFlag() == false) {
                    setTextInfo(smoking, R.color.colorPrimary, R.mipmap.smoking1);
                    flags.get(7).setFlag(true);
                } else {
                    setTextInfo(smoking, R.color.light_gray, R.mipmap.smoking);
                    flags.get(7).setFlag(false);
                }
                break;
        }
    }

    private void setTextInfo(TextView tv, Integer color, Integer img) {
        Drawable drawable = getResources().getDrawable(img);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tv.setCompoundDrawables(null, drawable, null, null);
        tv.setTextColor(getResources().getColor(color));
    }

    private void setTextInfo2(TextView tv, Integer color, Integer background) {
//        Drawable drawable = getResources().getDrawable(img);
//        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//        tv.setCompoundDrawables(null, drawable, null, null);
        tv.setTextColor(getResources().getColor(color));
        tv.setBackgroundResource(background);
    }
}
